<?php
	session_start();	
?>

<?php
	if( $_SESSION["admin"] != "" )
	{
?>
<a href="index.php?com=template"><b>List template</a></b> | <b><a href="index.php?com=template&func=add">Add new template</b></a>
<br />
<?php 
	include_once("process/template.php");
	$template = new template();
	$path = rtrim($_SERVER['PHP_SELF'],"index.php")."/library/fckeditor/";
    
    if( isset($_POST['btnAdd']) )
	{	
		$file = $_FILES["template_image"];
		$file_name = $file["name"];
		$file_ext = explode(".",$file_name);
		if($file["name"] == "")
		{			
			$name = stripcslashes($_POST["template_name"]);
			$description=stripcslashes($_POST["description"]);
			$templatepath=stripcslashes($_POST["template_path"]);
            if($name!= "" && $templatepath != "")
            {
			$template->AddTemplate($name , $templatepath, $description, $file["name"]);
						
			echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=template" />';
			return;
            }
		}

		if($file_ext[count($file_ext)-1] == "jpg" || $file_ext[count($file_ext)-1] == "jpeg" || $file_ext[count($file_ext)-1] == "gif")
		{										
			
			$name = stripcslashes($_POST["template_name"]);
			$description=stripcslashes($_POST["template_description"]);
			$templatepath=stripcslashes($_POST["template_path"]);
			$template->AddTemplate($name ,$templatepath , $description, $file["name"]);
			
			move_uploaded_file($_FILES["template_image"]["tmp_name"],"../asset/".$_FILES["template_image"]["name"]);
			
			echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=template" />';
			return;
		}
		else
		{
			$alert='<script type="text/javascript">
			alert("You are only allowed to select the file format jpg, jpeg, gif");
			</script>';
			echo $alert;
		}
	}
?>

<form method="post" action="index.php?com=template&func=add" id="contactform" name="addtemplate" onSubmit="return validateForm()">
	<table width="70%" style="font-size:10pt;">
		<tr>
			<td align="center" colspan="4" height="50"><b style="font-size:12pt; color:Blue;">Add new template</b></td>
		</tr>
		<tr style="font-weight:bold;">
			<td align="right">Template name :&nbsp;&nbsp;</td>
			<td>
				<input type="text" class="validate[required]" name="template_name" id="template_name" /><font style="color: red;"> *</font>
			</td>
		</tr>
        	<tr style="font-weight:bold;">
			<td align="right">Template path :&nbsp;&nbsp;</td>
			<td>
				<input type="text" class="validate[required]" name="template_path" id="template_path" /><font style="color: red;"> *</font>
			</td>
		</tr>
        
        <tr style="font-weight:bold;">
			<td align="right">Template description :&nbsp;&nbsp;</td>
			<td>
                <?php
				include("library/fckeditor/fckeditor.php");
            	$summary = new FCKeditor("template_description");
				$summary->BasePath = $path;
				$summary->Height=300;
				$summary->Value = "";
				$summary->Create();
			?>
            <font style="color: red;"> *</font>
			</td>
		</tr>
        	<tr style="font-weight:bold;">
			<td align="right">Template image :&nbsp;&nbsp;</td>
			<td>
                <input type="file" name="template_image" id="template_image" /><font style="color: red;"> *</font>
			</td>
		</tr>
		<tr>
			<td></td>
			<td colspan="2" align="left" height="40">
				<input type="submit" name="btnAdd" id="btnAdd" value="Add"  />
				<input type="reset" value="Reset" />
			</td>
		</tr>
	</table>
</form>
<i>Please fill in these fields (<font style="color: red;">*</font>)</i>
<?php
    }
   else
	{
		echo "<meta http-equiv='refresh' content='0; url=login.php' />";
	}
?>